<?php
	class Question_topic extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($name = 0, $school_id = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$name = $post_data['name'] ? urldecode($post_data['name']) : 0;
			}else{
				$name = urldecode($name);
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('name' => $name, "school_id" => $school_id);
			$this->load->model("Question_topics");
			$result = $this->Question_topics->get_question_topic_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/question_topic/index/' . $name . '/' . $school_id;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 5;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("action", '话题列表');
			$this->smarty->assign("name", $name);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("question_topic_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("type", 8);
            $school_info['id'] = $school_id;
            $this->smarty->assign("school_info", $school_info);
			$this->smarty->display("question/topic_list.html");
		}

		function status($id = 0, $status = 0){
			if($id > 0 && ($status == 0 || $status == 1)){
				$this->db->where("id", $id);
				$this->db->update("question_topics", array("status" => $status));
				show_message($_SERVER['HTTP_REFERER']);
			}else{
				show_message($_SERVER['HTTP_REFERER'], "非法的请求");
			}
		}

		function qlist($tid, $school_id = 0, $limit = 0){
			if($tid > 0){
				$tinfo = get_field_by_where("question_topics", "name, school_id", "id='{$tid}'");
				if(!$tinfo){
					show_message($_SERVER['HTTP_REFERER'], "话题不存在");exit;
				}
				$name = $tinfo['name'];
			}else{
				$name = "无话题";
			}
			
			$this->load->model("Question_topics");
			$result = $this->Question_topics->get_question_tlist($tid, $school_id, $limit, $this->offset);

			$this->load->library('pagination');
			$page_config['base_url'] = '/question_topic/qlist/' . $tid;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("action", $name.'问答列表');
			$this->smarty->assign("qlist", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("question/qlist.html");
		}

		function delete_qlist($id = 0){
			if(intval($id) > 0){
				delete_data((array)$id, "questions");
				show_message($_SERVER['HTTP_REFERER']);
			}else{
				show_message($_SERVER['HTTP_REFERER'], "非法操作");
			}
		}
	}